package com.practice.response;

import lombok.Data;

/**
 * @author kayn
 * @create 2023-10-09 上午 10:53
 */
@Data
public class ResponseR<T>  {
    private Boolean success;
    private Integer code;
    private String message;
    private T data;

    public static ResponseR<?> ok() {
        ResponseR<?> r = new ResponseR<>();
        r.setSuccess(ResponseEnum.SUCCESS.getSuccess());
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMessage());
        return r;
    }

    public static ResponseR<?> okWithMsg(String msg) {
        ResponseR<?> r = new ResponseR<>();
        r.setSuccess(ResponseEnum.SUCCESS.getSuccess());
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }

    public static <T> ResponseR<T> ok(T data) {
        ResponseR<T> r = new ResponseR<>();
        r.setSuccess(ResponseEnum.SUCCESS.getSuccess());
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }

    public static ResponseR<?> error() {
        ResponseR<?> r = new ResponseR<>();
        r.setSuccess(Boolean.FALSE);
        return r;
    }

    public static ResponseR<?> errorWithMsg(String msg) {
        ResponseR<?> r = error();
        r.setMessage(msg);
        return r;
    }

    public ResponseR data(T obj) {
        this.setData(obj);
        return this;
    }

    public ResponseR message(String message) {
        this.setMessage(message);
        return this;
    }
}
